<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编号" prop="supplierCommodityNo">
        <el-input v-model="queryParams.supplierCommodityNo" placeholder="请输入商品编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="supplierCommodityName">
        <el-input v-model="queryParams.supplierCommodityName" placeholder="请输入商品名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品类别" prop="supplierCommodityType">
        <el-cascader ref="commodityType" :options="options" v-model="supplierCommodityType" :props="props"
          clearable></el-cascader>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <!--商品类型数据-->
      <el-col :span="3" :xs="24">
        <div class="head-container">
          <el-tree :data="commodityTypeOptions" :props="defaultProps" :expand-on-click-node="false" ref="tree"
            node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--商品数据-->
      <el-col :span="21" :xs="24">
        <el-table v-loading="loading" :data="ErpCommodityList"  row-key="supplierCommodityId" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
          <el-table-column label="商品编号" align="center" prop="supplierCommodityNo" />
          <el-table-column label="商品名称" align="center" prop="supplierCommodityName" />
          <!-- <el-table-column label="商品图片" align="center" prop="supplierCommodityImg" width="100">
            <template slot-scope="scope">
              <image-preview :src="scope.row.supplierCommodityImg" :width="50" :height="50" />
            </template>
          </el-table-column> -->
          <el-table-column label="商品类别" align="center" prop="commodityTypeName" />
          <el-table-column label="规格" align="center" prop="supplierCommoditySpec" />
          <el-table-column label="单位" align="center" prop="supplierCommodityUnit" />
          <el-table-column label="商品报价" align="center" prop="supplierCommodityQuotation">
            <template slot-scope="scope">
              <span>{{ formattedNumber(scope.row.supplierCommodityQuotation) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { listErpSupplierCommodity } from "@/api/erp/ErpSupplierCommodity";
import { listErpCommodityType } from "@/api/erp/ErpCommodityType";
import {formattedNumber} from "@/utils/ruoyi";
export default {
  name: "ErpCommodity",
  props: {
    supplierId: {
      type: Number
    },
    notSupplierCommodityIds: {
      type: Array
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品管理表格数据
      ErpCommodityList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        supplierId: this.supplierId,
        supplierCommodityName: null,
        supplierCommodityNo: null,
        supplierCommodityType: null,
        params: {
          notSupplierCommodityIds: this.notSupplierCommodityIds
        }
      },
      supplierCommodityType: [],
      // 商品类别配置选项
      props: {
        multiple: true,
        checkStrictly: true,
        value: 'commodityTypeId',
        label: 'commodityTypeName'
      },
      // 左侧商品类别树
      commodityTypeOptions: [],
      // 下拉商品类型
      options: [],
      defaultProps: {
        children: "children",
        label: "commodityTypeName"
      },
    };
  },
  created() {
    this.getList();
    this.getListCommodityType();
  },
  methods: {
    formattedNumber,
    /** 查询商品管理列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      listErpSupplierCommodity(this.queryParams).then(response => {
        this.ErpCommodityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取商品类别 */
    getListCommodityType() {
      listErpCommodityType().then(response => {
        this.options = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions = [];
        const data = { commodityTypeId: 0, commodityTypeName: '全部', children: [] };
        data.children = this.handleTree(response.data, "commodityTypeId", "commodityTypePid");
        this.commodityTypeOptions.push(data);
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      let nodes = this.$refs.commodityType.getCheckedNodes();
      if (nodes.length > 0 && this.supplierCommodityType.length > 0) {
        let nodeIds = nodes.map((item) => item.value);
        this.queryParams.supplierCommodityType = nodeIds.join("|");
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.supplierCommodityType = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commodityId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.$emit('SelectionChange', selection);
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.supplierCommodityType =
        data.commodityTypeId == 0 ? null : data.commodityTypeId;
      this.handleQuery();
    },
    handleTypeChange(e) {
      let nodesObj = this.$refs['cascader'].getCheckedNodes();
      if (nodesObj.length > 0) {
        let code = [];
        nodesObj[0].pathNodes.forEach(i => {
          code.push(i.data.commodityTypeNo);
        })
      }
    },
    /** 数值累计 */
    numberSum(list) {
      let sum = 0;
      list.forEach(i => {
        sum += i.commodityNumber;
      })
      return sum;
    }

  }
};
</script>
<style lang="css" scoped>
  ::v-deep .el-table__cell{
    padding: 0px;
  }
</style>
